package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.interceptor.LoginAuthInterceptor;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.common.ResultCodeEnum;
import com.atguigu.spzx.model.entity.sys.SysMenu;
import com.atguigu.spzx.model.entity.sys.SysUser;
import com.atguigu.spzx.model.exception.SpzxException;
import com.atguigu.spzx.model.vo.sys.MenuVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Resource
    SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> findNestdMenus() {
        //1、查询所有的菜单
        List<SysMenu> sysMenus = sysMenuMapper.findAll();
        //2、挑选出1级菜单集合
        List<SysMenu> parentMenus = sysMenus.stream()
                //pid为0的是一级菜单
                .filter(sysMenu -> sysMenu.getParentId().longValue() == 0l)
                .collect(Collectors.toList());
        //3、遍历1级菜单，给每个1级菜单查找二级菜单组装
        buildMenuTree(sysMenus , parentMenus);
        //三级菜单
        return parentMenus;
    }

    @Override
    public void deleteById(Long id) {
        Integer count = sysMenuMapper.countChildrenById(id);
        //只能删除没有子菜单的菜单
        if(count>0){
            throw new SpzxException(ResultCodeEnum.MENU_DELETE_ERROR,null);
        }
        sysMenuMapper.deleteById(id);
    }

    @Override
    public SysMenu getById(Long id) {
        return sysMenuMapper.getById(id);
    }

    @Override
    public void updateById(SysMenu sysMenu) {
        //更新和新增时 菜单的标题不能重复
        sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu);
    }

    @Override
    public List<SysMenu> findDynamicMenus() {
        SysUser sysUser = LoginAuthInterceptor.THREAD_LOCAL.get();

        List<SysMenu> menus = sysMenuMapper.findDynamicMenus(sysUser.getId());
        List<SysMenu> parents = menus.stream().filter(menuVo -> menuVo.getParentId().longValue() == 0l)
                .collect(Collectors.toList());
        buildMenuTree(menus, parents);
        return parents;
    }

    //递归给父菜单挑选下一级菜单集合
    private void buildMenuTree(List<SysMenu> all , List<SysMenu> parents){
        if(CollectionUtils.isEmpty(parents)){
            return;
        }
        parents.forEach(parent->{
            List<SysMenu> children = all.stream().filter(sysMenu ->
                            sysMenu.getParentId().longValue() == parent.getId().longValue())
                    .collect(Collectors.toList());
            //children如果为null不会影响前端显示 但是如果为空集合
            if(!CollectionUtils.isEmpty(children)){
                parent.setChildren(children);
            }
            //children可能也有下一级菜单
            buildMenuTree(all , children);
        });
    }
}
